﻿using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using News.CYT369.Com.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using News.CYT369.Com.Domains;
using News.CYT369.Com.Service;

namespace News.CYT369.Com.Controllers
{
    public class HomeController : AllowAnonymousController
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index(string id)
        {
            int total = 0;
            int page = 1;
            int rows = 19;
            if (!string.IsNullOrEmpty(id))
            {
                var argsJson = Base64.Base64ToJSON(id);
                page = argsJson.GetInteger("Page", 1);
            }

            var docs = DocumentService.Instance.GetDocuments(page, rows, ref total);

            ViewBag.page = page;
            ViewBag.rows = rows;
            ViewBag.total = total;
            ViewBag.total_page = total / rows + (total % rows > 0 ? 1 : 0);

            return View(docs);
        }


        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
